﻿using Dora.Tools.Insurance.AYB.Dto;
using Dora.Tools.Utility;
using Dora.Tools.Utility.Const.ApiSetting;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Text;

namespace Dora.Tools.Insurance.AYB
{
    /// <summary>
    /// 安游保接口对接帮助类
    /// </summary>
    public class AYBInsuranceHelper
    {
        /// <summary>
        /// 2.1 投保接口
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApplyResponseDto Apply(string appId, string sign, ApplyInfoData data)
        {
            ApplyRequestDto requestDto = new ApplyRequestDto();
            requestDto.service_name = "apply"; // 服务名
            requestDto.app_id = appId;
            requestDto.sign = DEncryptHelper.GetMD5(sign).ToLower();
            requestDto.data = data;
            RestRequest request = new RestRequest("", Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
            request.AddJsonBody(requestDto);

            var dto = RestHelper.Execute<ApplyResponseDto>(InsuranceConstValue.AYB_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 2.2  退保接口
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <param name="data"></param>
        public static CancelResponseDto Cancel(string appId, string sign, CancelDataDto data)
        {
            CancelRequestDto requestDto = new CancelRequestDto();
            requestDto.service_name = "cancel"; // 服务名
            requestDto.app_id = appId;
            requestDto.sign = DEncryptHelper.GetMD5(sign).ToLower();
            requestDto.data = data;

            RestRequest request = new RestRequest("", Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
            request.AddJsonBody(requestDto);

            var dto = RestHelper.Execute<CancelResponseDto>(InsuranceConstValue.AYB_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 2.3 订单查询接口
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <param name="bizOrderSn">第三方唯一流水编号（订单号）</param>
        public static OrderResponseDto OrderQuery(string appId, string sign, string bizOrderSn)
        {
            OrderRequestDto requestDto = new OrderRequestDto();
            requestDto.service_name = "orderQuery"; // 服务名
            requestDto.app_id = appId;
            requestDto.sign = DEncryptHelper.GetMD5(sign).ToLower();

            requestDto.data = new OrderQueryData();
            requestDto.data.biz_order_sn = bizOrderSn;

            RestRequest request = new RestRequest("", Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
            request.AddJsonBody(requestDto);

            var dto = RestHelper.Execute<OrderResponseDto>(InsuranceConstValue.AYB_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 2.4 保费查询接口
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static QueryPremiumResponseDto QueryPremium(string appId, string sign, QueryPremiumData data)
        {
            QueryPremiumRequestDto requestDto = new QueryPremiumRequestDto();
            requestDto.service_name = "queryPremium"; // 服务名
            requestDto.app_id = appId;
            requestDto.sign = DEncryptHelper.GetMD5(sign).ToLower();
            requestDto.data = data;

            RestRequest request = new RestRequest("", Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
            request.AddJsonBody(requestDto);

            var dto = RestHelper.Execute<QueryPremiumResponseDto>(InsuranceConstValue.AYB_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 2.5 可用额度查询
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="sign"></param>
        /// <returns></returns>
        public static AvailableBalanceResponeDto GetAvailableBalance(string appId, string sign)
        {
            AvailableBalanceRequestDto requestDto = new AvailableBalanceRequestDto();
            requestDto.service_name = "getAvailableBalance"; // 服务名
            requestDto.app_id = appId;
            requestDto.sign = DEncryptHelper.GetMD5(sign).ToLower();

            RestRequest request = new RestRequest(string.Empty, Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=utf-8;");
            request.AddJsonBody(requestDto);

            var dto = RestHelper.Execute<AvailableBalanceResponeDto>(InsuranceConstValue.AYB_BaseUrl, request);
            return dto;
        }
    }
}
